<script>
    var edit = function () {
        return $('#edit').serializeArray();
    };
    layui.use(['table', 'jquery', 'layer'], function () {
        var table = layui.table, $ = layui.jquery, layer = layui.layer;
        table.on('tool(tableEvent)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                var url = "{:url('delete')}" + '/id/' + data.id;
                layer.confirm('你确定要此操作吗?', {
                    icon: 5,
                    btn: ['确定', '取消'] //按钮
                }, function () {
                    $.get(url, function (data) {
                        layer.msg(data.msg);
                        if (data.code == 1) {
                            obj.del();
                        }
                    });
                });
            } else if (obj.event == 'edit') {
                var url = "{:url('edit')}" + '/id/' + data.id;
                var title = '修改';
                sub(title, url)
            } else if (obj.event == 'show') {
                // sub(2, data.id); 详情
                console.log(data.id)
            }
        });
    });

    function sub(title, url) {
        layui.use(['jquery', 'layer'], function () {
            var $ = layui.jquery, layer = layui.layer;
            var index = layer.open({
                title: title
                , type: 2
                , area: ['80%', '80%']
                , btn: ['确定', '取消']
                , isOutAnim: true
                , closeBtn: false
                , content: url
                , yes: function (index, layero) {
                    var fields = $(layero).find('iframe')[0].contentWindow.edit();
                    $.ajax({
                        type: 'POST',
                        dateType: 'json',
                        url: "{:url('save')}",
                        data: fields,
                        success: function (data) {
                            layer.msg(data.msg);
                            layer.close(index);
                            // location.reload();　　//layer.open关闭刷新
                        }
                    });
                }
            })
        })
    }

    $('.add').click(function () {
        // sub(1, '');
        var title = '添加';
        var url = "{:url('edit')}";
        sub(title, url)
    });
 </script>
</body>
</html>